package com.haidechizi.fastadmin.modules.gen.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haidechizi.fastadmin.common.page.PageDto;
import com.haidechizi.fastadmin.common.utils.PageInfo;
import com.haidechizi.fastadmin.common.utils.Response;
import com.haidechizi.fastadmin.common.validator.ValidatorUtils;
import com.haidechizi.fastadmin.modules.gen.entity.GenScheme;
import com.haidechizi.fastadmin.modules.gen.entity.GenTable;
import com.haidechizi.fastadmin.modules.gen.service.GenSchemeService;
import com.haidechizi.fastadmin.modules.gen.service.GenTableService;
import com.haidechizi.fastadmin.modules.gen.util.GenUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 生成方案Controller
 */
@RestController
@RequestMapping(value = "gen/genScheme")
public class GenSchemeController {

    @Autowired
    private GenSchemeService genSchemeService;

    @Autowired
    private GenTableService genTableService;


    @RequiresPermissions("gen:genScheme:list")
    @RequestMapping(value = "list")
    public Response list(PageDto pageDto, GenScheme genScheme) {
        PageInfo page = genSchemeService.findPage(pageDto, genScheme);
        return Response.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("gen:genScheme:info")
    public Response info(@PathVariable("id") Long id) {
        GenScheme genScheme = genSchemeService.selectById(id);
        return Response.ok().put("genScheme", genScheme)
                .put("tableList", genTableService.selectList(new EntityWrapper<>()));
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("gen:genScheme:save")
    public Response save(@RequestBody GenScheme genScheme) {
        //校验类型
        genScheme.setSubModuleName("");
        ValidatorUtils.validateEntity(genScheme);
        String message = genSchemeService.save(genScheme);
        return Response.ok(message);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("gen:genScheme:update")
    public Response update(@RequestBody GenScheme genScheme) {
        //校验类型
        ValidatorUtils.validateEntity(genScheme);
        genScheme.setSubModuleName("");
        String message = genSchemeService.update(genScheme);
        return Response.ok(message);
    }


    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @RequiresPermissions("gen:genScheme:delete")
    @RequestMapping(value = "delete")
    public Response delete(@RequestBody Long[] ids) {
        genSchemeService.deleteBatchIds(Arrays.asList(ids));
        return Response.ok();
    }

    /**
     * 获取已添加的表列表
     *
     * @return
     */
    @RequiresPermissions("gen:genScheme:info")
    @RequestMapping(value = "tableList")
    public Response tableList() {
        List<GenTable> tableList = genTableService.selectList(new EntityWrapper<>());
        GenScheme genScheme = new GenScheme();
        genScheme.setPackageName("com.haidechizi.fastadmin.modules");
        return Response.ok().put("tableList", tableList).put("genScheme", genScheme);

    }

    /**
     * 字典列表
     *
     * @return
     */
    @RequiresPermissions("gen:genScheme:info")
    @RequestMapping(value = "dictConfig")
    public Response dictConfig() {
        return Response.ok()
                .put("generatorTypeList", GenUtils.getConfig().getCategoryList());
    }

}
